package Design;

import javax.swing.*;
import java.awt.event.*;

public class LoginFrame extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;

    public LoginFrame() {

        super("客运专线信息管理系统登录");               // 设置窗口标题
        setSize(400, 200);                  // 设置窗口大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // 设置窗口关闭操作
        JPanel panel = new JPanel();                     // 创建面板
        placeComponents(panel);
        add(panel);                                      // 添加面板到窗口中
        setLocationRelativeTo(null);                     // 设置窗口居中显示
    }
    private void placeComponents(JPanel panel) {
        panel.setLayout(null);
        // 创建用户名标签
        JLabel userLabel = new JLabel("用户名:");
        userLabel.setBounds(65, 20, 80, 25);
        panel.add(userLabel);

        // 创建用户名文本框
        usernameField = new JTextField(20);
        usernameField.setBounds(140, 20, 165, 25);
        panel.add(usernameField);

        // 创建密码标签
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(65, 50, 80, 25);
        panel.add(passwordLabel);

        // 创建密码文本框
        passwordField = new JPasswordField(20);
        passwordField.setBounds(140, 50, 165, 25);
        panel.add(passwordField);

        // 创建登录按钮
        JButton loginButton = new JButton("登录");
        loginButton.setBounds(160, 100, 80, 25);
        panel.add(loginButton);

        // 添加登录按钮的点击事件监听器
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 获取输入的用户名和密码
                String enteredUsername = usernameField.getText();
                String enteredPassword = new String(passwordField.getPassword());
                // 验证用户名和密码
                if (enteredUsername.equals("1") && enteredPassword.equals("1")) {
                    JOptionPane.showMessageDialog(LoginFrame.this, "登录成功！");
                    dispose(); // 关闭当前窗口
                    new MainFrame().setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(LoginFrame.this, "用户名或密码错误，请重试！");
                }
            }
        });
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // 创建登录界面实例
                LoginFrame loginFrame = new LoginFrame();
                // 设置窗口可见
                loginFrame.setVisible(true);
            }
        });
    }
}